package com.android.clinic.app.ui.widget.popwindow;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.clinic.app.R;
import com.android.clinic.app.entity.DuePriceInfo;
import com.android.clinic.app.ui.widget.listener.DialogOnItemListener;
import com.android.clinic.app.utils.DisplayUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2018/9/28.
 */
public class TimeLimitPopupWindow extends CommonPopupWindow implements AdapterView.OnItemClickListener {

    private LayoutGravity layoutGravity;
    private ListView listView;
    private DialogListAdapter adapter;
    private DialogOnItemListener onItemListener;

    public TimeLimitPopupWindow(Context c) {
        super(c, R.layout.popupwindow_list, DisplayUtil.dip2px(120), DisplayUtil.dip2px(150));
    }

    public void setItems(List<DuePriceInfo> items) {
        adapter.setData(items);
        adapter.notifyDataSetChanged();
    }

    public void setOnItemListener(DialogOnItemListener listener) {
        onItemListener = listener;
    }

    @Override
    protected void initView() {
        layoutGravity = new LayoutGravity(LayoutGravity.ALIGN_RIGHT);
        layoutGravity.setHoriGravity(LayoutGravity.ALIGN_RIGHT);
        layoutGravity.setVertGravity(LayoutGravity.TO_BOTTOM);

        contentView.setBackgroundResource(R.drawable.corner_popup_bg);
        adapter = new DialogListAdapter(context);
        int padding = DisplayUtil.dip2px(10);
        contentView.setPadding(padding, padding, padding, padding);
        listView = (ListView) contentView.findViewById(R.id.listView);
        listView.setDivider(new ColorDrawable(Color.parseColor("#DCDCDC")));
        listView.setDividerHeight(DisplayUtil.dip2px(1));
        listView.setAdapter(adapter);
    }

    @Override
    protected void initEvent() {
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mInstance.dismiss();
        if (onItemListener != null) {
            onItemListener.onItemClick(position);
        }
    }

    public void show(View anchor) {
        showBashOfAnchor(anchor, layoutGravity, 0, 20);
    }

    class DialogListAdapter extends BaseAdapter {

        private Context mContext;
        private List<DuePriceInfo> items = new ArrayList<>();

        public DialogListAdapter(Context context) {
            this.mContext = context;
        }

        public void setData(List<DuePriceInfo> items) {
            this.items = items;
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public DuePriceInfo getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pop_list, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvText.setText(items.get(position).getRemark());

            return convertView;
        }

        class ViewHolder {
            TextView tvText;

            public ViewHolder(View view) {
                tvText = (TextView) view.findViewById(R.id.text);
                tvText.setGravity(Gravity.CENTER);
            }
        }


    }
}
